package com.ssy.lingxi.product.serviceImpl.customer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.common.constant.Constants;
import com.ssy.lingxi.common.constant.product.CategoryEntity;
import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.constant.tree.TreeUtils;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.model.vo.SelectVO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.component.redis.service.IRedisStringUtils;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.product.entity.commodity.Commodity;
import com.ssy.lingxi.product.entity.customer.CustomerAttribute;
import com.ssy.lingxi.product.entity.customer.CustomerCategory;
import com.ssy.lingxi.product.entity.platform.Category;
import com.ssy.lingxi.product.repository.commodity.CommodityRepository;
import com.ssy.lingxi.product.repository.customer.CustomerCategoryRepository;
import com.ssy.lingxi.product.repository.platform.CategoryRepository;
import com.ssy.lingxi.product.service.customer.ICustomerCategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 品类实现类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
@Service
public class CustomerCategoryServiceImpl implements ICustomerCategoryService {

    @Resource
    private CategoryRepository categoryRepository;

    @Resource
    private CustomerCategoryRepository customerCategoryRepository;

    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private IRedisStringUtils redisStringUtils;


    /**
     * 同步平台品类
     * @return
     */
    @Transactional
    @Override
    public Boolean syncCategory(UserLoginCacheDTO sysUser, SimpleIdListRequest simpleIdListRequest) {
        //获取参数
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        List<Long> categoryIdList = simpleIdListRequest.getIdList();
        //查询数据库最新数据
        List<Category> categoryList = categoryRepository.findByIdIn(categoryIdList);
        List<CustomerCategory> customerCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleId(memberId, memberRoleId);
        //定义变量，缓存对应关系
        Map<Long,String> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId,Category::getName));
        Map<Long,String> customerCategoryMap = customerCategoryList.stream().collect(Collectors.toMap(CustomerCategory::getId,CustomerCategory::getName));
        HashMap<String, CustomerCategory> existMap = new HashMap<>();
        customerCategoryList.forEach(customerCategory -> {
            String[] split = customerCategory.getFullId().split(Constants.TRANSFER_STR + Constants.SPLIT_STR_FULL_ID);
            String fullName = Arrays.stream(split).map(categoryId -> customerCategoryMap.get(Long.parseLong(categoryId))).collect(Collectors.joining(Constants.SPLIT_STR_FULL_ID));
            existMap.put(fullName, customerCategory);
        });
        HashMap<Long, Long> map = new HashMap<>();
        List<CustomerCategory> newCustomerCategoryList = new ArrayList<>();
        categoryList.forEach(category -> {
            String[] split = category.getFullId().split(Constants.TRANSFER_STR + Constants.SPLIT_STR_FULL_ID);
            String categoryFullName = Arrays.stream(split).map(categoryId -> categoryMap.get(Long.parseLong(categoryId))).collect(Collectors.joining(Constants.SPLIT_STR_FULL_ID));
            //判断节点是否存在，不存在需要新增
            CustomerCategory newCustomerCategory = existMap.get(categoryFullName);
            if(newCustomerCategory == null){
                newCustomerCategory = new CustomerCategory();
                newCustomerCategory.setCategory(category);
                newCustomerCategory.setName(category.getName());
                newCustomerCategory.setLevel(category.getLevel());
                newCustomerCategory.setImageUrl(category.getImageUrl());
                newCustomerCategory.setType(category.getType());
                newCustomerCategory.setFullId(category.getFullId());
                newCustomerCategory.setParentId(category.getParentId());
                newCustomerCategory.setMemberId(sysUser.getMemberId());
                newCustomerCategory.setMemberName(sysUser.getCompany());
                newCustomerCategory.setMemberRoleId(sysUser.getMemberRoleId());
                newCustomerCategory.setMemberRoleName(sysUser.getMemberRoleName());
                newCustomerCategory.setUserId(sysUser.getUserId());
                newCustomerCategory.setUserName(sysUser.getUserName());
                newCustomerCategory.setCreateTime(System.currentTimeMillis());
                customerCategoryRepository.saveAndFlush(newCustomerCategory);
                newCustomerCategoryList.add(newCustomerCategory);
            }
            map.put(category.getId(), newCustomerCategory.getId());
        });
        //更新parentId和fullId
        List<CustomerCategory> newList = newCustomerCategoryList.stream().peek(customerCategory -> {
            Long categoryParentId = customerCategory.getParentId();
            Long customerCategoryParentId = map.get(categoryParentId);
            customerCategory.setParentId(customerCategoryParentId == null ? 0 : customerCategoryParentId);
            String fullId = customerCategory.getFullId();
            String[] split = fullId.split(Constants.TRANSFER_STR + Constants.SPLIT_STR_FULL_ID);
            String customerCategoryFullId = Arrays.stream(split).map(categoryId -> {
                Long customerCategoryId = map.get(Long.parseLong(categoryId));
                return String.format(Constants.CATEGORY_FULL_ID_NUM, customerCategoryId);
            }).collect(Collectors.joining(Constants.SPLIT_STR_FULL_ID));
            customerCategory.setFullId(customerCategoryFullId);
        }).collect(Collectors.toList());
        if(!newList.isEmpty()){
            customerCategoryRepository.saveAll(newList);
            //重新初始化品类信息
            this.initToRedis();
        }
        return true;
    }

    /**
     * 查询品类树
     * @return Wrapper<?>
     * @param rootNodeId
     */
    @Override
    public List<Node> getCustomerCategoryTree(UserLoginCacheDTO sysUser, Long rootNodeId){
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        //查询数据库数据
        List<CustomerCategory> customerCategoryList;
        if(rootNodeId != null && rootNodeId > 0){
            customerCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndFullIdStartingWith(memberId, memberRoleId, String.format(Constants.CATEGORY_FULL_ID_NUM, rootNodeId));
        }else{
            customerCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleId(memberId, memberRoleId);
        }

        if(!customerCategoryList.isEmpty()){
            //组装树结构
            List<Node> sourceList = customerCategoryList.stream().map(c -> new Node(String.valueOf(c.getId()), String.valueOf(c.getParentId()), c.getName(),false, c.getImageUrl(), c.getSort(), c.getSendCycle(), new ArrayList<>())).collect(Collectors.toList());
            List<Node> resultList = customerCategoryList.stream().filter(c -> c.getLevel() == 1).map(c -> new Node(String.valueOf(c.getId()), String.valueOf(c.getParentId()), c.getName(),false, c.getImageUrl(), c.getSort(), c.getSendCycle(), new ArrayList<>())).collect(Collectors.toList());
            return TreeUtils.buildTree(sourceList, resultList);
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 查询品类树--供应商
     * @param memberId 供应商id
     * @param memberRoleId 供应商角色id
     * @param rootNodeId 根节点的id,空代表查询全部
     * @param excludeType 排除品类类型 品类类型：1-实物商品、2-虚拟商品、3-服务商品、4-积分兑换商品
     * @return
     */
    @Override
    public List<Node> getMemberCustomerCategoryTree(Long rootNodeId, Long memberId, Long memberRoleId, Integer excludeType) {
        //查询数据库数据
        List<CustomerCategory> customerCategoryList;
        if(rootNodeId != null && rootNodeId > 0){
            customerCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndFullIdStartingWith(memberId, memberRoleId, String.format(Constants.CATEGORY_FULL_ID_NUM, rootNodeId));
        }else{
            customerCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleId(memberId, memberRoleId);
        }

        if(!customerCategoryList.isEmpty()){
            //过滤品类
            if (excludeType != null){
                customerCategoryList =   customerCategoryList.stream().filter(customerCategory -> !excludeType.equals(customerCategory.getType())).collect(Collectors.toList());
            }
            //组装树结构
            List<Node> sourceList = customerCategoryList.stream().map(c -> new Node(String.valueOf(c.getId()), String.valueOf(c.getParentId()), c.getName(),false, c.getImageUrl(), c.getSort(), c.getSendCycle(), new ArrayList<>())).collect(Collectors.toList());
            List<Node> resultList = customerCategoryList.stream().filter(c -> c.getLevel() == 1).map(c -> new Node(String.valueOf(c.getId()), String.valueOf(c.getParentId()), c.getName(),false, c.getImageUrl(), c.getSort(), c.getSendCycle(), new ArrayList<>())).collect(Collectors.toList());
            return TreeUtils.buildTree(sourceList, resultList);
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 查询品类
     * @param pageVO
     * @param name
     * @return Page<Attribute>
     */
    @Override
    public Page<CustomerCategory> getCustomerCategoryList(UserLoginCacheDTO sysUser, PageVO pageVO, String name){
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize());
        return customerCategoryRepository.findByMemberIdAndMemberRoleIdAndNameContainingOrderByIdDesc(sysUser.getMemberId(), sysUser.getMemberRoleId(), name, page);
    }

    /**
     * 查询品类
     * @param id
     * @return CustomerCategory
     */
    @Override
    public CustomerCategory getCustomerCategory(Long id){
        return customerCategoryRepository.findById(id).orElse(null);
    }

    /**
     * 添加/修改品类
     * @param customerCategory
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public String saveOrUpdateCustomerCategory(UserLoginCacheDTO sysUser, CustomerCategory customerCategory){
        String defaultFullId = String.format(Constants.CATEGORY_FULL_ID_NUM, 0);
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        Long userId = sysUser.getUserId();
        Long id = customerCategory.getId();
        String name = customerCategory.getName();
        Long parentId = customerCategory.getParentId();
        List<CustomerCategory> old_category;
        //检验名称和key是否重复
        if(id != null && id > 0){
            CustomerCategory current_customerCategory = customerCategoryRepository.findById(id).orElse(null);
            if(current_customerCategory == null){
                throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_NOT_EXIST);
            }
            Integer level = current_customerCategory.getLevel();
            Long currentParentId = current_customerCategory.getParentId();
            old_category = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndLevelAndParentIdAndNameAndIdNot(memberId, memberRoleId, level, currentParentId, name, id);
            //保留跟属性的关联关系
            customerCategory.setCustomerAttributeList(current_customerCategory.getCustomerAttributeList());
        }else{
            if(parentId != null && parentId > 0){
                CustomerCategory parentCategory = customerCategoryRepository.findById(parentId).orElse(null);
                if(parentCategory != null){
                    Integer level = parentCategory.getLevel();
                    old_category = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndLevelAndName(memberId, memberRoleId, level + 1, name);
                }else{
                    throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_PARENT_NOT_EXIST);
                }
            }else{
                old_category = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndLevelAndName(memberId, memberRoleId, 1, name);
            }

        }
        //判断同一等级的品类名称是否重复
        if(!old_category.isEmpty()){
            throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_NAME_EXIST);
        }
        //判断父节点是否存在
        if(parentId != null && parentId > 0){
            CustomerCategory parentCustomerCategory = customerCategoryRepository.findById(parentId).orElse(null);
            if(parentCustomerCategory == null){
                throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_PARENT_NOT_EXIST);
            }
            customerCategory.setLevel(parentCustomerCategory.getLevel() + 1);
            String fullId = parentCustomerCategory.getFullId();
            customerCategory.setFullId(fullId);
        }else{
            customerCategory.setLevel(1);
            customerCategory.setParentId(0L);
            customerCategory.setFullId(String.format(Constants.CATEGORY_FULL_ID_NUM, 0));
        }
        //后台品类
        Category category = customerCategory.getCategory();
        if(category != null){
            Long categoryId = category.getId();
            if(categoryId != null && categoryId > 0){
                Category current_Category = categoryRepository.findById(categoryId).orElse(null);
                if(current_Category != null){
                    customerCategory.setCategory(current_Category);
                }else{
                    throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_PLATFORM_NOT_EXIST);
                }
            }
        }
        customerCategory.setMemberId(memberId);
        customerCategory.setMemberName(sysUser.getCompany());
        customerCategory.setMemberRoleId(memberRoleId);
        customerCategory.setMemberRoleName(sysUser.getMemberRoleName());
        customerCategory.setUserId(userId);
        customerCategory.setUserName(sysUser.getUserName());
        //数据库持久化对象
        customerCategoryRepository.saveAndFlush(customerCategory);
        String fullId = customerCategory.getFullId();
        if(StringUtils.isNotEmpty(fullId) && !defaultFullId.equals(fullId)){
            customerCategory.setFullId(fullId + Constants.SPLIT_STR_FULL_ID + String.format(Constants.CATEGORY_FULL_ID_NUM, customerCategory.getId()));
        }else{
            customerCategory.setFullId(String.format(Constants.CATEGORY_FULL_ID_NUM, customerCategory.getId()));
        }
        //数据库持久化对象
        customerCategoryRepository.saveAndFlush(customerCategory);

        //将品类信息缓存到redis
        redisStringUtils.hSet(Constants.REDIS_KEY_CUSTOMER_CATEGORY, String.valueOf(customerCategory.getId()), JSONUtil.toJsonStr(BeanUtil.copyProperties(customerCategory, CategoryEntity.class)), Constants.REDIS_PRODUCT_INDEX);
        return ResponseCode.SUCCESS.getMessage();
    }

    /**
     * 删除品类
     * @param id
     * @return
     */
    @Override
    public String deleteCustomerCategory(UserLoginCacheDTO sysUser, Long id) {
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        CustomerCategory customerCategory = customerCategoryRepository.findById(id).orElse(null);
        if(customerCategory != null){
            //判断是否存在子节点
            List<CustomerCategory> subCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndParentId(memberId, memberRoleId, id);
            if(!subCategoryList.isEmpty()){
                throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_HAVESUB_NOT_DELETE);
            }
            //判断是否有商品还在使用
            List<Commodity> commodityList = commodityRepository.findByMemberIdAndMemberRoleIdAndCustomerCategoryId(memberId, memberRoleId, id);
            if(!commodityList.isEmpty()){
                throw new BusinessException(ResponseCode.PRODUCT_COMMODITY_RELATION_NOT_DELETE);
            }
            //判断是否跟属性绑定
            List<CustomerAttribute> customerAttributeList = customerCategory.getCustomerAttributeList();
            if(customerAttributeList != null && !customerAttributeList.isEmpty()){
                throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_HAVE_ATTRIBUTE_NOT_DELETE);
            }
            customerCategoryRepository.deleteById(id);
            //从redis删除品类
            redisStringUtils.hDel(Constants.REDIS_KEY_CUSTOMER_CATEGORY, String.valueOf(id), Constants.REDIS_PRODUCT_INDEX);
            //从redis删除品类属性排序
            redisStringUtils.del(Constants.REDIS_KEY_CUSTOMER_CATEGORY_ATTRIBUTE_SORT + id,Constants.REDIS_PRODUCT_INDEX);
            return ResponseCode.SUCCESS.getMessage();
        }
        throw new BusinessException(ResponseCode.PRODUCT_CATEGORY_NOT_EXIST);
    }

    /**
     * 查询品类下拉框 -- 商品能力
     * @param name 品类名称
     * @return
     */
    @Override
    public List<SelectVO> getSelectCustomerCategory(Long memberId, Long memberRoleId, String name) {
        //根据条件查询
        Pageable pageable = PageRequest.of(0, Constants.SELECT_COUNT);
        Page<CustomerCategory> page;
        if(StringUtils.isNotEmpty(name)){
            page = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndNameContainingOrderByIdDesc(memberId, memberRoleId, name, pageable);
        }else{
            page = customerCategoryRepository.findByMemberIdAndMemberRoleIdOrderByIdDesc(memberId, memberRoleId, pageable);
        }
        //转换SelectVO实体
        return page.getContent().stream().map(customerCategory -> {
            SelectVO selectVO = new SelectVO();
            selectVO.setId(customerCategory.getId());
            selectVO.setName(customerCategory.getName());
            return selectVO;
        }).collect(Collectors.toList());
    }

    /**
     * 查询品类下拉框 -- 当前会员
     * @param name 品类名称
     * @return
     */
    @Override
    public List<SelectVO> getCustomerCategory(Long memberId, Long memberRoleId, String name) {
        //根据条件查询
        List<CustomerCategory> list;
        if(StringUtils.isNotEmpty(name)){
            list = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndNameContainingOrderByIdDesc(memberId, memberRoleId, name);
        }else{
            list = customerCategoryRepository.findByMemberIdAndMemberRoleIdOrderByIdDesc(memberId, memberRoleId);
        }

        //转换SelectVO实体
        return list.stream().map(customerCategory -> {
            SelectVO selectVO = new SelectVO();
            selectVO.setId(customerCategory.getId());
            selectVO.setName(customerCategory.getName());
            return selectVO;
        }).collect(Collectors.toList());
    }

    /**
     * 查询会员品类最大级别
     * @return
     */
    @Override
    public Integer getMaxCustomerCategory(UserLoginCacheDTO sysUser) {
        return customerCategoryRepository.findMaxLevelByMemberId(sysUser.getMemberId());
    }

    /**
     * 根据等级查询会员品类
     * @param parentId
     * @return
     */
    @Override
    public List<SelectVO> getCustomerCategoryByParentId(UserLoginCacheDTO sysUser, Long parentId) {
        List<CustomerCategory> customerCategoryList = customerCategoryRepository.findByMemberIdAndMemberRoleIdAndParentId(sysUser.getMemberId(), sysUser.getMemberRoleId(), parentId);
        //转换SelectVO实体
        return customerCategoryList.stream().map(customerCategory -> {
            SelectVO selectVO = new SelectVO();
            selectVO.setId(customerCategory.getId());
            selectVO.setName(customerCategory.getName());
            return selectVO;
        }).collect(Collectors.toList());
    }

    /**
     * 通过fullId查找完整名称(包括所有父级节点名称)
     * @param fullId
     * @return
     */
    @Override
    public String getFullNameByFullId(String fullId) {
        if(fullId.contains(Constants.SPLIT_STR_FULL_ID)){
            String[] split = fullId.split(Constants.TRANSFER_STR + Constants.SPLIT_STR_FULL_ID);
            String[] idStr = Arrays.stream(split).map(s -> String.valueOf(Long.valueOf(s))).toArray(String[]::new);
            List<String> customerCategoryList = redisStringUtils.hMGet(Constants.REDIS_KEY_CUSTOMER_CATEGORY, idStr, Constants.REDIS_PRODUCT_INDEX);
            if(customerCategoryList != null && !customerCategoryList.isEmpty()){
                return StringUtils.join(customerCategoryList.stream().map(customerCategory -> {
                    if(StringUtils.isNotEmpty(customerCategory)){
                        JSONObject jsonObject = JSONUtil.parseObj(customerCategory);
                        return jsonObject.getStr("name");
                    }else{
                        return "";
                    }
                }).toArray(String[]::new),"->");
            }else{
                return "";
            }
        }else{
            String customerCategory = redisStringUtils.hMGet(Constants.REDIS_KEY_CUSTOMER_CATEGORY, String.valueOf(Long.valueOf(fullId)), Constants.REDIS_PRODUCT_INDEX);
            if(StringUtils.isNotEmpty(customerCategory)){
                JSONObject jsonObject = JSONUtil.parseObj(customerCategory);
                return jsonObject.getStr("name");
            }else{
                return "";
            }
        }
    }

    /**
     * 品类缓存到redis
     */
    @Async
    @Override
    public void initToRedis() {
        redisStringUtils.del(Constants.REDIS_KEY_CUSTOMER_CATEGORY, Constants.REDIS_PRODUCT_INDEX);
        List<CustomerCategory> categoryList = customerCategoryRepository.findAll();
        if(!categoryList.isEmpty()){
            Map<String, String> map = new HashMap<>();
            categoryList.forEach(customerCategory -> map.put(String.valueOf(customerCategory.getId()), JSONUtil.toJsonStr(BeanUtil.copyProperties(customerCategory, CategoryEntity.class))));
            redisStringUtils.hMSet(Constants.REDIS_KEY_CUSTOMER_CATEGORY, map, Constants.REDIS_PRODUCT_INDEX);
        }
    }

    /**
     * 通过后台品类id查询会员信息
     */
    @Override
    public List<Long> getMemberIdList(Long id) {
        List<CustomerCategory> customerCategoryList = customerCategoryRepository.findByCategoryId(id);
        return customerCategoryList.stream().map(CustomerCategory::getMemberId).collect(Collectors.toList());
    }

    /**
     * 通过会员id查询品类
     * @param memberIdList
     * @return
     */
    @Override
    public List<CustomerCategory> getCustomerCategoryByMemberId(List<Long> memberIdList) {
        return customerCategoryRepository.findByMemberIdIn(memberIdList);
    }

    /**
     * 通过会员id和会员角色id查询品类
     * @param memberIdList
     * @return
     */
    @Override
    public List<CustomerCategory> getCustomerCategoryByMemberIdAndMemberRoleId(List<Long> memberIdList, List<Long> memberRoleIdList) {
        return customerCategoryRepository.findByMemberIdInAndMemberRoleIdIn(memberIdList, memberRoleIdList);
    }

    /**
     * 通过id查询分类
     * @param idList
     * @return
     */
    @Override
    public List<CustomerCategory> getCustomerCategoryByIdList(List<Long> idList) {
        return customerCategoryRepository.findAllById(idList);
    }

    /**
     * 查询当前会员所有品类的名称
     * @return 品类名称集合
     */
    @Override
    public List<String> getCustomerCategoryNameList(UserLoginCacheDTO sysUser) {
        List<String> customerCategoryNameList = new ArrayList<>();
        Map<String, String> customerCategoryRedisMap = redisStringUtils.hGetAll(Constants.REDIS_KEY_CUSTOMER_CATEGORY, Constants.REDIS_PRODUCT_INDEX);
        customerCategoryRedisMap.forEach((key, value) -> {
            CustomerCategory customerCategory = JSONUtil.toBean(value, CustomerCategory.class);
            Long memberId = customerCategory.getMemberId();
            Long memberRoleId = customerCategory.getMemberRoleId();
            if(sysUser.getMemberId().equals(memberId) && sysUser.getMemberRoleId().equals(memberRoleId)){
                String fullId = customerCategory.getFullId();
                if(fullId.contains(".")){
                    StringBuilder stringBuilder = new StringBuilder();
                    String[] split = fullId.split("\\.");
                    int customerCategoryId = Integer.parseInt(split[split.length - 1]);
                    for (String s : split) {
                        String customerCategoryName = this.getCustomerCategoryName(Integer.parseInt(s), customerCategoryRedisMap);
                        if(StringUtils.isNotEmpty(customerCategoryName)){
                            stringBuilder.append(customerCategoryName).append("-");
                        }
                    }
                    if(stringBuilder.length() > 0){
                        customerCategoryNameList.add(customerCategoryId + "_" + stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("-")).toString());
                    }
                }else{
                    int customerCategoryId = Integer.parseInt(fullId);
                    String customerCategoryName = this.getCustomerCategoryName(customerCategoryId, customerCategoryRedisMap);
                    if(StringUtils.isNotEmpty(customerCategoryName)){
                        customerCategoryNameList.add(customerCategoryId + "_" + customerCategoryName);
                    }
                }
            }
        });
        return customerCategoryNameList.stream().sorted().collect(Collectors.toList());
    }

    /**
     * redis数据格式中获取品类名称
     * @param categoryId                    品类id
     * @param customerCategoryRedisMap      redis数据
     * @return 品类名称
     */
    private String getCustomerCategoryName(int categoryId, Map<String, String> customerCategoryRedisMap){
        String customerCategoryJson = customerCategoryRedisMap.get(String.valueOf(categoryId));
        if(StringUtils.isNotEmpty(customerCategoryJson)){
            CustomerCategory newCustomerCategory = JSONUtil.toBean(customerCategoryJson, CustomerCategory.class);
            return newCustomerCategory.getName();
        }else{
            return "";
        }
    }
}